import axios from 'axios'
import Vue from 'vue';
import store from '@/store';
import { Loading, Message } from 'element-ui';
var loadObj = null;
const service = axios.create({
    baseURL: 'http://localhost:3007',
    timeout: 10000
})
// 添加请求拦截器
service.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    config.headers['user-token'] = store.getters.token;
    if (!loadObj) {
        loadObj = Loading.service({
            text: "加载中...",
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)',
            lock: true
        })
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    Vue.nextTick(() => {
        loadObj.close();
    })
    if (response.data.code === 666) {
        return response;
    }
    else {
        Message({ message: response.data.msg, type: "error" });
        return Promise.reject(response.data.msg);
    }
}, function (error) {
    // 对响应错误做点什么
    Vue.nextTick(() => {
        loadObj.close();
    })
    return Promise.reject(error);
});
export default service;